#ifndef __HLW8032_H__
#define __HLW8032_H__

/**
 * - Port: UART1
 * - Receive (Rx) buffer: on
 * - Transmit (Tx) buffer: off
 * - Flow control: off
 * - Event queue: on
 * - Pin assignment: TxD (default), RxD (default)
 */
#define EX_UART_NUM UART_NUM_1
#define PATTERN_CHR_NUM (3) /*!< Set the number of consecutive and identical characters received by receiver which defines a UART pattern*/
#define HLW8032_TXD_PIN (GPIO_NUM_39)
#define HLW8032_RXD_PIN (GPIO_NUM_42)
#define BUF_SIZE (1024)
#define RD_BUF_SIZE (BUF_SIZE)
#define HLW8032_DEBUG 0

#define VOLTAGE_COEFFICIENT (1.88) /* 4*470kR */
#define CURRENT_COEFFICIENT (1.0)  /* 1mR */

#define HLW8032_NVS_NAME "HLW8032"
#define HLW8032_NVS_KEY "HLW_NVS_PF"

#ifdef __cplusplus
extern "C"
{
#endif

    typedef enum
    {
        EFFECTIVE_VOLTAGE,     /*!< effective_voltage*/
        EFFECTIVE_CURRENT,     /*!< effective_current*/
        ACTIVE_POWER,          /*!< active_power*/
        APPARENT_POWER,        /*!< apparent_power*/
        FACTOR,                /*!< factor*/
        PF_COUNT,              /*!< pf_count*/
        ONE_PULSES,            /*!< one_pulses*/
        EFFECTIVE_CALCULATION, /*!< electricity_calculation*/
    } hlw8032_get_data_type_t;

    typedef enum
    {
        BZERO_EFFECTIVE_VOLTAGE, /*!< effective_voltage*/
        BZERO_EFFECTIVE_CURRENT, /*!< effective_current*/
        BZERO_POWER,             /*!< active_power*/
        BZERO_PARAMETER,         /*!< effective_parameter*/
        BZERO_ALL_BZERO,         /*!< all_bzero*/
    } hlw8032_bzero_data_type_t;

    typedef struct
    {
        uint8_t state_reg;
        uint8_t check_reg;
        uint32_t voltage_par_reg;
        uint32_t voltage_reg;
        uint32_t current_par_reg;
        uint32_t current_reg;
        uint32_t power_par_reg;
        uint32_t power_reg;
        uint16_t pf_reg_sum;
        uint16_t pf_reg;
        uint8_t checksum_reg;
        bool PF_reg_sate_now;
        bool PF_reg_sate_last;
    } hlw8032_uart_handle_data;

    typedef struct
    {
        float effective_voltage;
        float effective_current;
        float active_power;
        float apparent_power;
        float factor;
        uint32_t pf_count;
        uint32_t one_pulses;
        float electricity_calculation;
    } hlw8032_data;

    float hlw8032_get_data(hlw8032_get_data_type_t data_type);
    void hlw8032_task(void *arg);

#ifdef __cplusplus
}
#endif

#endif
